package com.zengdada.authority.mapper;

import com.zengdada.authority.model.AppRole;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface AppRoleMapper extends Mapper<AppRole> {
    @Select("select r.code from syoauth.app_role r, syoauth.app_user_role_rel urr where r.id = urr.app_role_id and urr.app_user_id = #{userId,jdbcType=VARCHAR}")
    List<Map<String, Object>> selectRoleByUserId(@Param("userId") String id);

    @Select("<script>select DISTINCT * " +
            "  from (select * from syoauth.app_role r " +
            "         where r.state = 1 and r.application_id in ('0',#{applicationId, jdbcType = VARCHAR})" +
            " <if test=\"types!=null\"><foreach collection=\"types\" item=\"type\" open=\" and type in(\" separator=\",\" close=\")\"> #{type} </foreach></if>) s " +
            "  <if test=\"isSuperM != true\"> start with s.id in " +
            "            (select rr.app_role_id from syoauth.app_user_role_rel rr  where rr.app_user_id = #{userId, jdbcType = VARCHAR}) " +
            "  connect by prior s.id = s. parent_id </if> </script>")
    List<Map<String, Object>> selectRoleAndSon(@Param("userId") String userId, @Param("isSuperM") boolean isSuperAdministrator, @Param("applicationId") String application_id, @Param("types") List<String> types);
}